package com.wk.springboot.file.monitor;

import com.wk.springboot.file.filter.DiskReturnFilter;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * @Classname DiskSendMonitor
 * @Description 本地磁盘返回报文监听器
 * @Date 2019/11/27 11:14
 * @Author by weikai
 */

@Component
public class DiskReturnMonitor extends BaseMonitor implements InitializingBean {
  /**
   * 开启发送端disk监听
   */
  private void start() throws Exception {

    LOGGER.info("监听的发送端返回目录为：{}",SEND_RETURN_DIR);
    long interval = TimeUnit.MILLISECONDS.toMillis(INTERVAL_TIME);
    LOGGER.info("时间间隔：{}",interval);
    //不使用过滤器
    FileAlterationObserver observer = new FileAlterationObserver(new File(SEND_RETURN_DIR));
    observer.addListener(new DiskReturnFilter());
    LOGGER.info("======添加过滤器=======");
    //创建文件变化监听器
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
    // 开始监控
    monitor.start();
  }


  @Override
  public void afterPropertiesSet(){
    try{
      start();
    }catch (Exception e){
      LOGGER.error("+++++++++++++++++++++启动发送监听失败，原因是：{}",e.getMessage());
    }
  }
}
